<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /**
 * Function 设计链表
*/
    function MyLinkedList() {
      function Node(val) {
        this.val = val
        this.next = null
      }

      this.head = null
      this.length = 0

      MyLinkedList.prototype.get = index => {
        let cur = this.head
        // console.log(this.length)
        if (index >= 0 && index < this.length) {
          for (let i = 0; i < index; i++) {
            cur = cur.next
          }
          return cur.val
        }
        return -1
      }

      MyLinkedList.prototype.addAtHead = val => {
        const newNode = new Node(val)
        if (this.head === null) {
          this.head = newNode
        } else {
          const cur = this.head
          newNode.next = cur
          this.head = newNode
        }
        this.length += 1
        // console.log(this.length, '1111111111');
      }

      MyLinkedList.prototype.addAtTail = val => {
        const newNode = new Node(val)
        if (this.head === null) {
          this.head = newNode
        } else {
          let cur = this.head
          while (cur.next) {
            cur = cur.next
          }
          cur.next = newNode
          // console.log(this.length, '222222');
        }
        this.length += 1
      }

      MyLinkedList.prototype.addAtIndex = (index, val) => {
        const newNode = new Node(val)
        let cur = this.head

        if (index <= 0) {
          this.addAtHead(val)
        }

        if (index > 0 && index < this.length) {
          // console.log(index, this.length, '///////');
          for (let i = 0; i < index - 1; i++) {
            cur = cur.next
          }
          newNode.next = cur.next
          cur.next = newNode
          this.length += 1
        }

        if (index === this.length) {
          this.addAtTail(val)
          // console.log(this.length, '=========');
        }
      }

      MyLinkedList.prototype.deleteAtIndex = index => {
        if (index >= 0 && index < this.length) {
          let cur = this.head
          if (index === 0) {
            this.head = cur.next
          } else {
            for (let i = 0; i < index - 1; i++) {
              cur = cur.next
            }
            cur.next = cur.next.next
          }
          this.length -= 1
        }
      }
    }

    const newHead = new MyLinkedList()
    // newHead.addAtHead(7)
    // newHead.addAtHead(2)
    // newHead.addAtHead(1)
    // newHead.addAtIndex(3, 0)
    // newHead.deleteAtIndex(2)
    // newHead.addAtHead(6)
    // newHead.addAtTail(4)
    // console.log(newHead, '----------');
    // console.log(newHead.get(4))
    // newHead.addAtHead(4)
    // newHead.addAtIndex(5, 0)
    // newHead.addAtHead(6)
    // // 6 1 2 0 4 

    // newHead.addAtIndex(0, 10)
    // newHead.addAtIndex(0, 20)
    // newHead.addAtIndex(0, 30)
    // console.log(newHead.get())

    // newHead.addAtTail(1)
    // console.log(newHead.get(0))

    newHead.addAtIndex(1, 0)
    console.log(newHead.get(0), '=======')
  </script>
</body>

</html>